package com.ltw.playit.web.server.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltw.playit.common.bean.Criteria;
import com.ltw.playit.common.utils.WebUtils;
import com.ltw.playit.web.server.dao.BaseUsersDao;
import com.ltw.playit.web.server.entity.BaseUsers;
import com.ltw.playit.web.server.service.BaseUsersService;

@Service
public class BaseUsersServiceImpl implements BaseUsersService {
	
	@Autowired
	private BaseUsersDao baseUsersDao;
	
	private static final Logger logger = LoggerFactory.getLogger(BaseUsersServiceImpl.class);
	
	@Override
	public String login(Criteria criteria) {
		// 条件查询
		List<BaseUsers> list = this.baseUsersDao.findByUserName(criteria.getAsString("userName"));
		if (null == list || list.size() != 1) {
			// 没有此用户名
			return "00";
		}
		BaseUsers dataBaseUser = list.get(0);
		//错误3次,并且时间未到
		
		
		if (dataBaseUser.getErrorCount() >= 3 && compareTo(dataBaseUser.getLastLoginTime())) {
			return "请你联系管理员，或者一小时之后再次尝试！";
		}
		// 传入的password已经md5过一次了,并且为小写，加入salt值
		String passwordIn = WebUtils.encrypt(criteria.getAsString("passwordIn"), criteria.getAsString("userName"));
		if (!passwordIn.equals(dataBaseUser.getPassword())) {
			// 密码不正确
			return loginTimes(dataBaseUser,criteria);
			//return "00";
		}
		// 更新最后登录时间和登录ip
		BaseUsers updateUser = new BaseUsers();
		updateUser.setUserId(dataBaseUser.getUserId());
		updateUser.setErrorCount((short)0);
		updateUser.setLastLoginTime(new Date());
		updateUser.setLastLoginIp(criteria.getAsString("loginip"));
		this.baseUsersDao.save(updateUser);
		// controller中取出放到session中
		criteria.put("baseUser", dataBaseUser);
		return "01";
	}
	
	/**
	 * 限制密码输入次数
	 * 
	 * @param baseUsers
	 * @param criteria
	 * @return
	 */
	private String loginTimes(BaseUsers baseUsers, Criteria criteria) {
		String info = "";
		Short errorCount = baseUsers.getErrorCount();
		Date lastLoginTime = baseUsers.getLastLoginTime();
		if(!compareTo(lastLoginTime)){//已经过了XX分钟，那么把errorCount设置为0，重新计数。
			errorCount=(short)0;
		}
		switch (errorCount) {
		case 0:
			// 第一次输入密码错误
			info = "密码错误!你还有2次机会输入密码!";
			errorCount = (short) (errorCount + 1);
			break;
		case 1:
			// 第二次输入密码错误
			info = "密码错误!你还有1次机会输入密码!<br/>如果输入错误，帐户将被锁定不能登录！";
			errorCount = (short) (errorCount + 1);
			break;
		case 2:
			// 第三次输入密码错误
			info = "密码错误!你的帐户已经被锁定！<br/>请联系管理员！";
			errorCount = (short) (errorCount + 1);
			break;
		default:
			break;
		}
		// 保存新的错误次数和时间
		BaseUsers updateUser = new BaseUsers();
		updateUser.setUserId(baseUsers.getUserId());
		updateUser.setErrorCount(errorCount);
		updateUser.setLastLoginTime(new Date());
		updateUser.setLastLoginIp(criteria.getAsString("loginip"));
		this.baseUsersDao.save(updateUser);
		return info;
	}
	
	/**
	 * 日期比较
	 * 
	 * @param date
	 * @return
	 */
	private boolean compareTo(Date date) {
		Calendar c = Calendar.getInstance();
		long now = c.getTimeInMillis();
		c.setTime(date);
		long lastly = c.getTimeInMillis();
		// 60分钟1000*60*60;
		return (now - lastly) <= 3600000;
	}

	@Override
	public String saveBaseUser(Criteria criteria) {
		BaseUsers user = (BaseUsers) criteria.get("user");
		this.baseUsersDao.save(user);
		return "01";
	}

}